package edu.gatech.oad;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FindUser extends Activity {

	private String name;
	private TextView user_name, user_type, lock_status;
	private Button findUserButton, toggleLock, deleteUser;
	private DataBaseHelper data = new DataBaseHelper(getBaseContext());
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.find_user);

		findUserButton = (Button) findViewById(R.id.finduserbutton);
		findUserButton.setOnClickListener(new OnClickListener() {


			@Override
			public void onClick(View v) {
				
	    		
	        	try {
	        		data.createDataBase();
	        		data.openDataBase();
	        	} catch(Exception sqle) {
	        		throw new Error("Unable to open database");
	        	}
	        	
				name = ((EditText) findViewById(R.id.searchUser))						
						.getText().toString();
				System.out.println("Check User Exists" + data.userExists(name));
				if (data.userExists(name)) {
					// Find Labels
					user_name = (TextView) findViewById(R.id.username_field);
					user_type = (TextView) findViewById(R.id.user_type_field);
					lock_status = (TextView) findViewById(R.id.title_text);
					toggleLock = (Button) findViewById(R.id.toggle_lock_status);
					deleteUser = (Button) findViewById(R.id.delete_user_button);

					// Set Value of Labels
					user_name.setText(name);
					String userTypeDB = data.checkAdminStatus(name) ? "Admin"
							: "Regular";
					if(name.contains("admin")){
						userTypeDB = "Admin";
					}
					user_type.setText(userTypeDB);
					String lockStatusDB = data.userLocked(name) ? "Locked"
							: "Active";
					lock_status.setText(lockStatusDB);

					user_name.setVisibility(View.VISIBLE);
					user_type.setVisibility(View.VISIBLE);
					lock_status.setVisibility(View.VISIBLE);
					toggleLock.setVisibility(View.VISIBLE);
					deleteUser.setVisibility(View.VISIBLE);
					
					toggleLock.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							
							if(data.userLocked(name)) {
								data.unlockUser(name);
								lock_status.setText("Active");
							} else {
								data.lockUser(name);
								lock_status.setText("Locked");
							}
							
						}
					});
					
					deleteUser.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {	
							data.deleteUser(name);							
							user_name.setVisibility(View.GONE);
							user_type.setVisibility(View.GONE);
							lock_status.setVisibility(View.GONE);
							toggleLock.setVisibility(View.GONE);
							deleteUser.setVisibility(View.GONE);
						}
					});
				}
				System.out.println(data.userExists(name));
			}
		});
	}
}
